/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package au.edu.swin.unilearn.submission;

import au.edu.swin.unilearn.assignment.AssignmentEntity;
import au.edu.swin.unilearn.students.StudentEntity;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

/**
 *
 * @author DidierLeung
 */
@Entity
@NamedQueries({
@NamedQuery(name = "findAllSubmissions", query = "SELECT se FROM SubmissionEntity se"),
@NamedQuery(name = "findLastSubmissionNo", 
                        query = "SELECT MAX(se.submission_no) FROM SubmissionEntity se"),
@NamedQuery(name = "findAllUnmarkedSubmission", 
                        query="SELECT se FROM SubmissionEntity se WHERE se.submission_status LIKE :status")
})
@Table(name = "SUBMISSION")
public class SubmissionEntity implements Serializable
{
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer submission_no;
    @Temporal(value = TemporalType.TIMESTAMP)
    private Date submitted_date;
    @Column
    private String submission_file_url;
    @Column
    private double results;
    @Column
    private String detail_file_url;
    @Column
    private String submission_status;
    @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER, optional = false)
    @JoinColumns({
@JoinColumn(name = "ASSIGNMENT_NO", referencedColumnName = "ASSIGNMENT_NO", nullable = false),
@JoinColumn(name = "SUBJECT_CODE", referencedColumnName = "SUBJECT_CODE", nullable = false)
})
    private AssignmentEntity assignmentEntity;
    @ManyToOne
    @JoinColumn(name = "SIMS_ID", referencedColumnName = "SIMS_ID", nullable = false)
    private StudentEntity studentEntity;

    public SubmissionEntity()
    {
    }

    public SubmissionEntity(Date submitted_date, String submission_file_url,
            double results, String detail_file_url,
            String submission_status)
    {
        this();
        setSubmitted_date(submitted_date);
        setSubmission_file_url(submission_file_url);
        setResults(results);
        setDetail_file_url(detail_file_url);
        setSubmission_status(submission_status);
    }

    public Integer getSubmission_no()
    {
        return submission_no;
    }

    public void setSubmission_no(Integer submission_no)
    {
        this.submission_no = submission_no;
    }

    public Date getSubmitted_date()
    {
        return submitted_date;
    }

    public void setSubmitted_date(Date submitted_date)
    {
        this.submitted_date = submitted_date;
    }

    public String getSubmission_file_url()
    {
        return submission_file_url;
    }

    public void setSubmission_file_url(String submission_file_url)
    {
        this.submission_file_url = submission_file_url;
    }

    public double getResults()
    {
        return results;
    }

    public void setResults(double results)
    {
        this.results = results;
    }

    public String getDetail_file_url()
    {
        return detail_file_url;
    }

    public void setDetail_file_url(String detail_file_url)
    {
        this.detail_file_url = detail_file_url;
    }

    public String getSubmission_status()
    {
        return submission_status;
    }

    public void setSubmission_status(String submission_status)
    {
        this.submission_status = submission_status;
    }

    public AssignmentEntity getAssignmentEntity()
    {
        return assignmentEntity;
    }

    public void setAssignmentEntity(AssignmentEntity assignmentEntity)
    {
        this.assignmentEntity = assignmentEntity;
    }

    public StudentEntity getStudentEntity()
    {
        return studentEntity;
    }

    public void setStudentEntity(StudentEntity studentEntity)
    {
        this.studentEntity = studentEntity;
    }

    public Submission getSubmissionObject()
    {
        return new Submission(submission_no, submitted_date, submission_file_url,
                              results, detail_file_url, submission_status,
                              assignmentEntity.getAssignmentData(), null);
    }

    public void setSubmissionEntityData(Submission s)
    {
        setSubmission_no(s.getSubmission_no());
        setSubmitted_date(s.getSubmitted_date());
        setSubmission_file_url(s.getSubmission_file_url());
        setResults(s.getResults());
        setDetail_file_url(s.getDetail_file_url());
        setSubmission_status(s.getSubmission_status());
    }

    @Override
    public int hashCode()
    {
        return getSubmission_no();
    }

    @Override
    public boolean equals(Object object)
    {
        SubmissionEntity other = (SubmissionEntity) object;
        if (submission_no == other.getSubmission_no())
        {
            return false;
        }
        return true;
    }

    @Override
    public String toString()
    {
        return "au.edu.swin.mockup.SubmissionEntity[id=" + submission_no + "]";
    }
}
